package cn.edu.hznu.util.concurrent;

public class CustomLock {

	public class Counter {
		private Lock lock = new Lock();
		private int count = 0;

		public int inc() {
			try {
				lock.lock();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			int newCount = ++count;
			lock.unlock();
			return newCount;
		}
	}

	public class Lock {
		private boolean isLocked = false;

		public synchronized void lock() throws InterruptedException {
			while (isLocked) {
				wait();
			}
			isLocked = true;
		}

		public synchronized void unlock() {
			isLocked = false;
			notify();
		}
	}
	
	// Test: 在多个线程中同时使用给同一个Counter对象，并执行其inc()方法
}
